use std::{alloc::Layout, cell::RefCell, rc::Rc};

mod common;
mod core;

#[macro_use]
extern crate lazy_static;

fn main() {
    // println!("{}", Solution::a());

    core::core::Core::new().main();

    //词法分析、语法分析
    // let parser = core::parser::Parser::new("C:\\Users\\ThinkPad\\Desktop\\b\\main.cpp");
    // println!("parser:{:#?}", parser.root());
    //语义分析
    // core::semantic_analyzer::semantic_analyzer(root)
    // core::code_generator::CodeGenerator::new(&parser.root(), "C:\\Users\\ThinkPad\\Desktop\\b\\main.txt").generate_code();
    // let mut v = vec![9, 9,9,9,9,9,9,9,9];
    // let a = "1010".to_owned();
    // let b = "1011".to_owned();
    // let s = "abcd".to_string();
    // let res = Solution::my_sqrt(9);
    // println!("res:{:?}", res);

    // let obj = Solution::new(vec![1,2,3]);
    // println!("obj:{:?}", obj);
    // let ret_1: i32 = obj.pick_index();
    // println!("ret:{}", ret_1);
    // unsafe{
    //     let a = std::alloc::alloc(Layout::new::<String>());
    // }
    // let node = ListNode::from_vec(vec![]);
    // println!("node:{:?}", node);
    // let res = Solution::delete_duplicates(Some(Box::new(node)));
    // println!("res:{:?}", res);
    // let a = "kind"..;
    // let s = "kindhouse".to_string();
    // let a  = s.chars().collect::<Vec<_>>();
    // match a.as_slice() {
    //     &['k','i','n','d'] => {
    //         println!("kind");
    //     }
    //     _ => {
    //         println!("else");
    //     }
    // }
    // fn offset_one(a: &mut*mut SoTest) {
    //     unsafe {
    //         *a = a.add(1);
    //         std::ptr::write(*a, SoTest("vaov"));
    //     }
    // }
    // let mut v = vec![SoTest("123"), SoTest("vd"), SoTest("你好吗")];
    // let mut a = v.as_mut_ptr();
    // println!("{:p}", a);
    // offset_one(&mut a);
    // unsafe {
    //     println!("{:?}", *a.offset(0));
    //     println!("{:?}", *a.offset(1));
    // }
}

#[derive(Debug)]
struct SoTest(&'static str);
#[derive(Debug)]
struct Solution {}

#[allow(unused)]
impl Solution {
    pub fn number_of_boomerangs(points: Vec<Vec<i32>>) -> i32 {
        0
    }
    pub fn delete_duplicates(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
        let mut res = None;
        while let Some(cur) = head {
            if !Self::exist_ele(&res, cur.val) {
                //添加元素
                Self::add_ele(&mut res, cur.val);
            }
            head = cur.next;
        }
        res
    }
    #[allow(unused)]
    fn add_ele(res: &mut Option<Box<ListNode>>, ele: i32) {
        if res.is_none() {
          *res = Some(Box::new(ListNode::new(ele)));
          return;
        }
        let mut tmp = res.as_mut();
        let mut len = 0;
        while let Some(node) = tmp {
            tmp = node.next.as_mut();
            len += 1;
        }
        let mut tmp = res.as_mut();
        let mut i = 0;
        while let Some(node) = tmp {
            if i == len - 1 {
                let val = node.val;
                *node.as_mut() = ListNode {
                    val,
                    next: Some(Box::new(ListNode::new(ele))),
                };
                return;
            }
            tmp = node.next.as_mut();
            i += 1;
        }
    }
    #[allow(unused)]
    fn exist_ele(res: &Option<Box<ListNode>>, ele: i32) -> bool {
        let mut res = res.clone();
        let mut tmp = res.as_mut();
        while let Some(node) = tmp {
            if node.val == ele {
                return true;
            }
            tmp = node.next.as_mut();
        }
        false
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
    pub val: i32,
    pub left: Option<Rc<RefCell<TreeNode>>>,
    pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
    #[inline]
    pub fn new(val: i32) -> Self {
        TreeNode {
            val,
            left: None,
            right: None,
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}

impl ListNode {
    #[inline]
    #[allow(unused)]
    fn new(val: i32) -> Self {
        ListNode { next: None, val }
    }
    #[allow(unused)]
    fn from_vec(vals: Vec<i32>) -> Self {
        let mut res = ListNode { val: 0, next: None };
        if vals.len() == 0 {
            return res;
        }
        res = Self::new(vals[0]);
        Self::set_vec(&mut res.next, &vals[1..]);
        res
    }
    fn set_vec(data: &mut Option<Box<ListNode>>, vals: &[i32]) {
        if vals.len() == 0 {
            return;
        }
        *data = Some(Box::new(Self::new(vals[0])));
        // let a = data.as_mut().unwrap().next;
        Self::set_vec(&mut data.as_mut().unwrap().next, &vals[1..])
    }
}
